package com.neusoft.dibuy.io;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

public class File1 {
/** TODO
 *  1、所有的流都依赖字节流完成具体工作
 *  2、所有的功能流都有转换工作
 *  3、流的读写顺序必须保持一致
 *  4、所有的实现了序列化接口的类，都可以通过序列化流完成对象的持久化存储，反序列化流完成对象的恢复。
*/
    public static void main(String[] args) throws FileNotFoundException {
//        DataInputStream dis =new DataInputStream(  new FileInputStream("d:\\a\\c.dat") );

    }
    public static void main13(String[] args) throws IOException, ClassNotFoundException {
        File file = new File("d:\\a\\c.dat");
        FileInputStream fis =new FileInputStream(file);
        ObjectInputStream ois =new ObjectInputStream(fis);
        Dept dept = (Dept) ois.readObject();
        System.out.println(dept);
        ois.close();
        fis.close();
        System.out.println(4);
    }

    public static void main12(String[] args) throws IOException {
        File file = new File("d:\\a\\c.dat");
        if (file.exists()){
            file.delete();
        }
        file.createNewFile();
        FileOutputStream fos =new FileOutputStream(file);
        ObjectOutputStream oos =new ObjectOutputStream(fos);
        Dept dept = new Dept(10, "研发部", "太理科技楼");
        oos.writeObject(dept);
        oos.close();
        fos.close();
    }
    public static void main111(String[] args) throws IOException {
        File file = new File("d:\\a\\b.dat");
        FileInputStream fis =new FileInputStream(file);
        ObjectInputStream ois =new ObjectInputStream(fis);
        int i = ois.readInt();
        ois.close();
        fis.close();
        System.out.println(4);
    }
    public static void main10(String[] args) throws IOException {
//        流对象 --- 》  File ， Net， Memory，Print，Scanner，
//        功能型  ---> 字符流，Object流，
        File file = new File("d:\\a\\b.dat");
        if (file.exists()){
            file.delete();
        }
        file.createNewFile();
        FileOutputStream fos =new FileOutputStream(file);
        int  i =4;
        ObjectOutputStream oos =new ObjectOutputStream(fos);
        oos.writeInt(i);
        oos.close();
        fos.close();
    }

    public static void main9(String[] args) throws IOException {
//readline
        File file = new File("d:\\a\\test01.txt");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        HashMap<String, Integer> map = new HashMap<>();
//        切分
        String line = null;
        while ((line = br.readLine()) != null){
//            System.out.println(line);
            String[] words = line.split("-");
            String key =  words[3];
            if (map.containsKey(key)){
                Integer value = map.get(key);
                value++;
                map.put(key,value);
            }else{
                map.put(key,1);
            }
        }
        map.forEach((k,v)-> System.out.println(k + ":" + v));

//
    }
    public static void main8(String[] args) throws IOException {
        File file = new File("d:\\a\\c.txt");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line = null;
        while ((line = br.readLine()) != null){
            System.out.println(line);
        }
    }

    public static void main7(String[] args) throws IOException {
        File file = new File("d:\\a\\c.txt");
        byte[] bs  =new byte[ (int )Math.min(1024, file.length())];
        FileInputStream fis =new FileInputStream(file);
        int  i  =  -1;
        int index = 0;
        while ((i =  fis.read())>-1){
            if ( i ==13){
                System.out.println(new String(bs,0 , index));
                index = 0 ;
                continue;
            }
            if (i == 10 ){
                continue;
            }
            bs[index] =  (byte) i ;
            index ++;
        }
        System.out.println(new String(bs,0 , index));

    }

    public static void main6(String[] args) throws IOException {
        File file = new File("d:\\a\\c.txt");
        byte[] bs = new byte[ (int )Math.min(1024, file.length())  ];
//        搬运工
        FileInputStream fis =new FileInputStream(file);
        int  l =  fis.read(bs);
        String s =  new String(bs,0,l, StandardCharsets.UTF_8);
        System.out.println(s );
    }

    public static void main5(String[] args) throws IOException {
        File file = new File("d:\\a\\d.jpg  ");
//        最多读取字节数
        byte[] bs = new byte[ (int )Math.min(1024, file.length())  ];
        FileInputStream fis =new FileInputStream(file);
         int l = -1;
        while ( (l =  fis.read(bs) )>-1)  {
            System.out.println( new String(bs) );
        }
    }
    public static void main4(String[] args) throws IOException {
        File file =new File("d:\\a", "d.jpg");
        if (file.isFile() && file.exists() && file.canRead()){
            byte[] bs = new byte[100];
            FileInputStream fis =new FileInputStream(file);
            int  l  =  fis.read(bs,10,10);
            fis.close();
            System.out.println(l );
            for (byte b : bs) {
                System.out.println(b);
            }
        }else {
            return;
        }

    }

    public static void main3(String[] args) {
        String path = "d:\\a";
        showAll(path);
    }
    private static void showAll(String path) {
        File file = new File(path);
        if (file.isFile()) return;
        String[] list = file.list();
        for (String s : list) {
            String filepath =  file + File.separator + s;
            System.out.println(  filepath );
            showAll(filepath);
        }
    }

    public static void main1(String[] args) throws IOException {
        String path = File.separator + "Users" + File.separator + "John" + File.separator + "Desktop";
        System.out.println("File path: " + path);
        String classpath = System.getProperty("java.class.path");
        System.out.println(classpath);
// 问题
//        String[] paths = classpath.split( "/" );// ;
//        for (String p : paths) {
//            System.out.println("Classpath: " + p);
//        }
//        File file = new File("D:\\a");
//        String[] list = file.list();
//        for (String s : list) System.out.println(file +  File.separator  + s);



//        if (file.exists()){
//            file.delete();
//            System.out.println("File exists");
//        }
//        file.createNewFile();


//        file.mkdirs(); // 创建多级目录

    }
}

